package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.bean.User;
import com.itheima.common.R;
import com.itheima.common.pageParm;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderService;
import com.itheima.service.OrderdetailService;
import com.itheima.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Resource
    private OrderdetailService orderdetailService;
    @Resource
    private OrderService orderService;

    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders, HttpSession session) {

        User user = (User) session.getAttribute("user");

        int row = orderService.submit(orders, user);
        if (row > 0) {
            shoppingCartService.clean();

            return R.success("订单提交成功");
        } else {
            return R.error("订单提交失败！");
        }


    }




    @GetMapping("/userPage")
    public R userPage(pageParm pageParm) {
        IPage<OrdersDto> iPage = orderService.userPage(pageParm);
        System.out.println("iPage = " + iPage);

        return R.success(iPage);
    }

    /**
     * 订单分页查询
     * @param pageParm
     * @return
     */
    @GetMapping("/page")
    public R findPage(pageParm pageParm){

        IPage<Orders> page = orderService.findPage(pageParm);

        return R.success(page);

    }

}
